package com.wanmao.test.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanmao.test.R;
import com.wanmao.test.model.RankBean;

import java.util.List;


public class RankAdapter extends BaseAdapter {

    List<RankBean> list;
    Context context;

    public RankAdapter(List<RankBean> list, Context context) {
        this.list = list;
        this.context = context;
    }




    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int i) {
        return list.size() > 0 ? list.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return list.size() > 0 ? i : null;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_rank, null);
            viewHolder = new ViewHolder();
            viewHolder.ranking = (TextView)view.findViewById(R.id.tv_ranking);
            viewHolder.head = (ImageView) view.findViewById(R.id.img_head);
            viewHolder.name = (TextView)view.findViewById(R.id.tv_name);
            viewHolder.depat = (TextView)view.findViewById(R.id.tv_depat);
            viewHolder.number =(TextView) view.findViewById(R.id.tv_number);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        RankBean rankBean = list.get(i);
        if (i == 0){
            viewHolder.ranking.setBackground(ContextCompat.getDrawable(context,R.drawable.first));
            viewHolder.ranking.setText(null);
        }else if (i == 1){
            viewHolder.ranking.setBackground(ContextCompat.getDrawable(context,R.drawable.second));
            viewHolder.ranking.setText(null);
        }else if (i == 2){
            viewHolder.ranking.setBackground(ContextCompat.getDrawable(context,R.drawable.third));
            viewHolder.ranking.setText(null);
        }else {
            viewHolder.ranking.setBackground(null);
            viewHolder.ranking.setText(i + 1 + "");
        }
        if (rankBean.getName().equals("刘喆")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_1));
        }else if (rankBean.getName().equals("胡志刚")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_2));
        }else if (rankBean.getName().equals("牛原")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_3));
        }else if (rankBean.getName().equals("魏菊芳")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_4));
        }else if (rankBean.getName().equals("张奇")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_5));
        }else if (rankBean.getName().equals("白婧婧")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_6));
        }else if (rankBean.getName().equals("张春辉")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_7));
        }else if (rankBean.getName().equals("孙阔")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_8));
        }else if (rankBean.getName().equals("徐福")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_9));
        }else if (rankBean.getName().equals("周森")){
            viewHolder.head.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.head_10));
        }
        viewHolder.name.setText(rankBean.getName());
        viewHolder.depat.setText("(" + rankBean.getDepat() + ")");
        viewHolder.number.setText(rankBean.getNumber());
        return view;
    }

    class ViewHolder {

        TextView ranking;
        ImageView head;
        TextView name;
        TextView depat;
        TextView number;
    }
}
